<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>去除字符串左右二边的空格</title>
</head>
<body>
    <h2>测试去除字符串左右两边的空格</h2>
    <input type='text' id='strs' value='          需要过滤空格 '>
    <input type='button' id='rstrsBtn' value=' 过滤 '>
    <script type="text/javascript">
        window.onload = function(){
            var _rstrsBtn = document.getElementById("rstrsBtn"),//获取过滤按钮对象
                    _strs = document.getElementById("strs");	//获取被过滤元素

            _rstrsBtn.onclick = function(){//去除空格
                _strs.value = _strs.value.replace( /^(\s|\u00A0)+|(\s|\u00A0)+$/g, "" );//正则替换
            }
        };
    </script>
</body>
</html>